package com.bawan.vims.common.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bawan.vims.common.DBConnectionPool;

/**
 * <p> Title        : DBUtil.java </p>
 * <p> Description	: DBUtil.java </p>
 * <p> Copyright	: Copyright (c) 2015 by FITT</p>
 * <p> Author 		: JOCHEN ZHENG </p>
 * <p> Create		: 2015-1-5 11:31:52 </p> 
 * <p> version 		: 1.0 </p>
 */
public class DBUtil {
	private static final Logger logger = LoggerFactory.getLogger(DBUtil.class);
	
	private static String DRIVER = "com.mysql.jdbc.Driver";
	private static String URL = "jdbc:mysql://localhost:3306/test?useUnicode=true";
	private static String USER = "root";
	private static String PASSWORD = "";
	private static Connection conn = null;
	
	public static DBConnectionPool dbConnPools = new DBConnectionPool(DRIVER, URL, USER, PASSWORD, 5);
	
	/**
	 * 从数据库连接池中获得一个数据连接对象
	 * @return
	 */
	public static Connection getConn() {
		conn = dbConnPools.getConnection(20000);
		return conn;
	}
	
	/**
	 * 关闭有PreparedStatment和ResultSet的连接对象
	 * @param conn
	 * @param pstmt
	 * @param rs
	 */
	public static void close(Connection conn, PreparedStatement pstmt, ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				logger.error("DBUtil.close(Connection, PreparedStatement, ResultSet) closeResultSet --> error:" + e.getMessage(), e);
			}
		}
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				logger.error("DBUtil.close(Connection, PreparedStatement, ResultSet) closePreparedStatement --> error:" + e.getMessage(), e);
			}
		}
		//将连接对象交给连接池统一管理
		dbConnPools.freeConnection(conn);
	}
	
	/**
	 * 关闭有Statement和ResultSet的连接对象
	 * @param conn
	 * @param stmt
	 * @param rs
	 */
	public static void close(Connection conn, Statement stmt, ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				logger.error("DBUtil.close(Connection, Statement, ResultSet) closeResultSet --> error:" + e.getMessage(), e);
			}
		}
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				logger.error("DBUtil.close(Connection, Statement, ResultSet) closeStatement --> error:" + e.getMessage(), e);
			}
		}
		//将连接对象交给连接池统一管理
		dbConnPools.freeConnection(conn);
	}
	
	/**
	 * 关闭有PreparedStatment连接对象
	 * @param conn
	 * @param pstmt
	 */
	public static void close(Connection conn, PreparedStatement pstmt) {
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				logger.error("DBUtil.close(Connection, PreparedStatement) --> error:" + e.getMessage(), e);
			}
		}
		//将连接对象交给连接池统一管理
		dbConnPools.freeConnection(conn);
	}
	
	/**
	 * 关闭有Statement的连接对象
	 * @param conn
	 * @param stmt
	 */
	public static void close(Connection conn, Statement stmt) {
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				logger.error("DBUtil.close(Connection, Statement) --> error:" + e.getMessage(), e);
			}
		}
		//将连接对象交给连接池统一管理
		dbConnPools.freeConnection(conn);
	}
}
